package com.movie.ticket.sale.movieassembly.common;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MapToStringUtil {
    /**
     * Map转String工具
     * @param map
     * @param separator 分隔符
     * @param kvSplice  键值拼接符
     * @return
     */
    public static String mapToString(Map<?, ?> map, String separator, String kvSplice) {
        List<String> result = new ArrayList<>();
        map.entrySet().parallelStream().reduce(result, (first, second)->{
            first.add(second.getKey() + kvSplice + second.getValue());
            return first;
        }, (first, second)->{
            if (first == second) {
                return first;
            }
            first.addAll(second);
            return first;
        });

        return StringUtils.join(result, separator);
    }

    public static void main(String[] args) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("name", "hjz");
        map.put("value", "25");
        System.out.println(MapToStringUtil.mapToString(map, ",", ":"));
        String s= JSONObject.toJSONString(map);
        System.out.println("更改座位状态："+s);
        //name:hjz;value:25
    }

}
